package com.qwt.weather;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONObject;

import java.time.Instant;
import java.util.List;

/**
 * 天气工具类
 *
 * @author 秦先生
 */
public class WeatherUtil {

    /**
     * 主机地址
     */
    private static final String HOST = "d1.weather.com.cn";

    /**
     * 请求头
     */
    private static final String REFERER = "https://www.weather.com.cn/";

    /**
     * 请求地址
     */
    private static final String URL = "https://%s/%s/%s.html?_=%s";

    /**
     * 获取当前时间的13位时间戳
     *
     * @return long
     */
    private static long currentTimeMillis() {
        Instant instant = Instant.now();
        return instant.toEpochMilli();
    }

    /**
     * 创建天气请求对象
     *
     * @return HttpRequest
     */
    private static String weatherRequest(String areaCode) {
        if (StrUtil.isBlank(areaCode)) {
            areaCode = WeatherAreaConst.LESHAN_SHIZHONG;
        }
        String weatherUrl = String.format(URL, HOST, "sk_2d", areaCode, currentTimeMillis());
        return execute(weatherUrl, REFERER);
    }

    /**
     * 创建预警请求对象
     *
     * @return HttpRequest
     */
    private static String weatherAlarmRequest(String areaCode) {
        if (StrUtil.isBlank(areaCode)) {
            areaCode = WeatherAreaConst.LESHAN_SHIZHONG;
        }
        String weatherUrl = String.format(URL, HOST, "dingzhi", areaCode, currentTimeMillis());
        return execute(weatherUrl, HOST);
    }

    /**
     * 执行请求
     *
     * @param url     url
     * @param referer 请求头
     * @return
     */
    private static String execute(String url, String referer) {
        HttpRequest request = HttpRequest.get(url)
                .header("Referer", referer);
        String body = StrUtil.EMPTY;
        try (HttpResponse response = request.execute()) {
            int status = response.getStatus();
            if (HttpStatus.HTTP_OK != status) {
                return body;
            }
            // 处理数据
            body = response.body();
        } catch (Exception ignore) {
        }
        return body;
    }

    /**
     * 获取当前时间的天气情况
     *
     * @return WeatherInfo
     */
    public static WeatherInfo getCurrentWeather() {
        return getCurrentWeather(WeatherAreaConst.LESHAN_SHIZHONG);
    }

    /**
     * 获取当前时间的天气情况
     *
     * @return WeatherInfo
     */
    public static WeatherInfo getCurrentWeather(String areaCode) {
        // 执行请求
        String response = weatherAlarmRequest(areaCode);
        JSONObject cityWeatherJson = WeatherParser.parseVar2Json(response, "cityDZ" + areaCode);

        WeatherInfo weatherInfo = new WeatherInfo();
        // 设置当日气象数据
        WeatherParser.setCityDzIntoWeather(cityWeatherJson, weatherInfo);
        // 设置当日预警数据
        JSONObject alarmJson = WeatherParser.parseVar2Json(response, "alarmDZ" + areaCode);
        List<AlarmInfo> alarms = WeatherParser.getAlarmDzIntoWeather(alarmJson);
        weatherInfo.setAlarms(alarms);

        // 获取实时气象数据
        String currentResponse = weatherRequest(areaCode);
        JSONObject currentJson = WeatherParser.parseVar2Json(currentResponse, "dataSK");
        WeatherParser.setDataSkIntoWeather(currentJson, weatherInfo);
        return weatherInfo;
    }

    /**
     * 获取天气预警数据
     *
     * @return AlarmInfo
     */
    public static List<AlarmInfo> getAlarm() {
        return getAlarm(WeatherAreaConst.LESHAN_SHIZHONG);
    }


    /**
     * 获取天气预警数据
     *
     * @return AlarmInfo
     */
    public static List<AlarmInfo> getAlarm(String areaCode) {
        // 执行请求
        String response = weatherAlarmRequest(areaCode);
        // 设置当日预警数据
        JSONObject alarmJson = WeatherParser.parseVar2Json(response, "alarmDZ" + areaCode);
        return WeatherParser.getAlarmDzIntoWeather(alarmJson);
    }

}
